from DButils import dbMysql, dbRedis,dbMongo
import datetime,json
from datetime import timedelta,date

class MarketModel():
    def __init__(self):
        pass

    @classmethod
    def DailyIncomeQueryByTimeInterval(cls, startDate, endDate):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_daily_income_interval.format(startDate,endDate))

    @classmethod
    def MonthlyIncomeQuery(cls):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_daily_income)

    @classmethod
    def GoodsTotalIncomeQueryByOrder(cls):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_goods_total_income_order)

    @classmethod
    def GoodsTotalIncomeQuery(cls):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_goods_total_income)

    @classmethod
    def DailyGoodsIncomeQuery(cls,date=None):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_daily_goods_income.format(date))

    @classmethod
    def DailyHourlyGoodsIncomeQuery(cls,date=None):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_daily_hourly_income.format(date))

    @classmethod
    def VipDailyExpense(cls,date=None):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_vip_daily_expense.format(date))

    @classmethod
    def VipDailyExpenseByUser(cls,user=None):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_daily_expense_vip.format(user))
        
    @classmethod
    def VipTotalExpense(cls):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_vip_total_expense)

    @classmethod
    def VipGoodsFavorite(cls):
        return dbMysql.db_Mysql.QueryDB(sql=tp_SQL.sql_vip_favorate_goods)
    
    @classmethod
    def DataQueryMysql(cls, sql=None):
        res = dbMysql.db_Mysql.QueryDB(sql)
        if res:data={'data':[item for item in res]}
        else:data={'data':'null'}
        return data
    
     
    @classmethod
    def DataQueryRedis(cls, sql=None,key=None,item=None):
        print("start to read from redis...")
        res = dbRedis.DBRedis().QueryDB(key=key,name=item)
        if not res:
            print("start to read from mysql first...")
            res=json.dumps(cls.DataQueryMysql(sql=sql))
            dbRedis.DBRedis().SetDB(key=key,field=item,value=res)
        return res
        
    @classmethod
    def DataQueryMongo(cls, sql=None,key=None):
        print("start to read from mongo...")
        res = dbMongo.DBMongo().QueryDB(collection=key)
        if not res:
            print("start to read from mysql first...")
            res=cls.DataQueryMysql(sql=sql)
            dbMongo.DBMongo().SetData(collection=key,params=res)
        return res
        
        
m=MarketModel()
# res=m.DataQueryRedis(sql=tp_SQL.sql_vip_favorate_goods.format('vip5'),key='9200',item='009')
# print(res)